package com.fancy.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fancy.common.dto.response.Result;
import com.fancy.user.dto.request.*;
import com.fancy.user.dto.response.UserManageListResponseVO;
import com.fancy.user.service.UserManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/admin/users")
@Api(tags = "用户管理服务")
@RequiredArgsConstructor
public class UserManageController {

    private final UserManageService userManageService;

    @GetMapping
    @ApiOperation("获取用户管理列表")
    public Result<Page<UserManageListResponseVO>> getUserList(
            @ApiParam("搜索关键词") @RequestParam(required = false) String keyword,
            @ApiParam("用户状态") @RequestParam(required = false) Integer status,
            @ApiParam("会员类型") @RequestParam(required = false) String membershipType,
            @ApiParam("开始日期") @RequestParam(required = false) String startDate,
            @ApiParam("结束日期") @RequestParam(required = false) String endDate,
            @ApiParam("页码") @RequestParam(defaultValue = "1") int page,
            @ApiParam("每页大小") @RequestParam(defaultValue = "20") int size) {

        return userManageService.getUserManageList(
                keyword, status, membershipType, startDate, endDate, page, size);
    }

    @PutMapping("/{userId}/manage")
    @ApiOperation("管理用户")
    public Result<Void> manageUser(
            @ApiParam("用户ID") @PathVariable Long userId,
            @RequestBody UserManageRequestDTO dto) {
        return userManageService.manageUser(userId, dto);
    }

    @DeleteMapping("/{userId}")
    @ApiOperation("删除用户")
    public Result<Void> deleteUser(
            @ApiParam("用户ID") @PathVariable Long userId,
            @ApiParam("删除原因") @RequestParam(required = false) String deleteReason) {
        return userManageService.deleteUser(userId, deleteReason);
    }

    @DeleteMapping("/batch")
    @ApiOperation("批量删除用户")
    public Result<Void> batchDeleteUsers(@RequestBody BatchDeleteUsersRequestDTO dto) {
        return userManageService.batchDeleteUsers(dto);
    }

    @PutMapping("/{userId}/password")
    @ApiOperation("重置用户密码")
    public Result<Void> resetUserPassword(
            @ApiParam("用户ID") @PathVariable Long userId,
            @RequestBody ResetPasswordRequestDTO dto) {
        return userManageService.resetUserPassword(userId, dto);
    }

    @PutMapping("/{userId}/membership")
    @ApiOperation("设置用户会员")
    public Result<Void> setUserMembership(
            @ApiParam("用户ID") @PathVariable Long userId,
            @RequestBody SetUserMembershipRequestDTO dto) {
        return userManageService.setUserMembership(userId, dto);
    }
}
